<?php
class myTools
{
  public static function langFromCulture($culture)
  {
    list($lang, $tmp) = explode('_', $culture);
    return $lang;
  }
  
  public static function now()
  {
  	return strftime(sfConfig::get('app_datefmt_set'));
  } 
   
  public static function prependTitle($title, $i18n=false)
  {
  	$sf_response = sfContext::getInstance()->getResponse();
  	$sf_title = $sf_response->getTitle();
  	$prepend = $i18n ? sfContext::getInstance()->getI18N()->__($title) : $title;
  	$separator = sfConfig::get('app_site_separator');
  	
  	$sf_response->setTitle($prepend.$separator.$sf_title);
  }
  
  public static function byDirection($ltr, $rtl)
  {
  	switch (sfContext::getInstance()->getUser()->getDirection())
  	{
  	  case 'rtl':
  	  	return $rtl;
  	  	break;
  	 
  	  default:
  	  	return $ltr;
  	  	break;
  	}
  }
  
  public static function dateWithinRange($date, $range)
  {
  	if (!$date)
  	{
  	  return false;
  	}
  	
  	return ((strtotime($date) + $range) > strtotime('now')) ? true : false;
  }
  
  public static function getSite($url)
  {
  	$elements = parse_url($url);
  	if ($elements['host'])
  	{
  	  //return $elements['scheme'].'://'.$elements['host'].'/';
  	  return $elements['host'];
  	}
  	
  	return false;
  }
  
  /**
   * Generates salt for password hashing
   *
   * @param string  $string         User specific string
   * @return string                 md5 hash
   */
  public static function makeSalt($string = false)
  {
  	$rand = $string ? rand(100000, 999999).$string : self::makeRandomString();
    return md5($rand);
  }
  
  /**
   * Generates rendom string
   *
   * @return string       8 character long string of readable characters
   */
  public static function makeRandomString()
  {
    $string = '';	  
    for ($i=0; $i<8; $i++)
    {
      $string .= chr(mt_rand(35, 126));
    }
    return $string;
  }
  
  public static function makeHandle($string)
  {
    $handle = mb_convert_case(strip_tags($string), MB_CASE_LOWER);
    $handle = mb_ereg_replace('[\W]', ' ', $handle);
    $handle = mb_eregi_replace('[\s]{2,}', ' ', $handle);
    $handle = mb_substr($handle, 0, 100);
    $handle = mb_eregi_replace('[\s]', '-', trim($handle));

    return $handle;
  }
  
  /*
   * Returns current internal uri
   * 
   * An interface for sfRouting class
   */
  public static function getCurrentInternalUri()
  {
  	$current_internal_uri = false;
  	
    $routing = sfContext::getInstance()->getRouting();
    $raw_uri = $routing->getCurrentInternalUri();
    // getCurrentInternalUri(true) retruns route name
    // getCurrentInternalUri(false) returns module/action
    
    $params = array();
    $url_params = array();
    
    if (preg_match('/module=/i', $raw_uri))
    {
      return $raw_uri;
    }
    else
    {
      $params = explode('?', $raw_uri);
      $url_params = explode('/', $params[0]);
      $module = $url_params[0];
      $action = $url_params[1];
      $current_internal_uri = '@'.$routing->getCurrentRouteName().'?module='.$module.'&action='.$action;
      if (array_key_exists(1, $params))
      {
      	$param_string = $params[1];
      }
      else
      {
      	$param_string = false;
      }
      
      $current_internal_uri = $param_string ? $current_internal_uri.'&'.$param_string : $current_internal_uri;      
    }
    
    return $current_internal_uri;
  }
  
  public static function getUploadDir($user, $type)
  {
  	return '/uploads/'.$user.'/'.$type;
  }
}